﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;

namespace TTApp.Phone.Silverlight.Hexagons.Pages
{
    public partial class GameplayPage : PhoneApplicationPage
    {
        #region //--------- Public Constructors ---------//
        public GameplayPage()
        {
            InitializeComponent();
            BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(OnBackKeyPress);
        }
        #endregion

        #region //--------- CLR Properties --------------//
        public Gameplay Gameplay
        {
            set
            {
                gameplayGrid.Children.Clear();
                gameplayGrid.Children.Add(value);
            }
            get
            {
                return gameplayGrid.Children[0] as Gameplay;
            }
        }
        #endregion

        #region //--------- Overridden Methods ----------//
        /// <summary>
        /// Xem kiểu chơi của Gameplay và khởi tạo.
        /// </summary>
        /// <param name="args"></param>
        protected override void OnNavigatedTo(NavigationEventArgs args)
        {
            //App app = Application.Current as App;
            //if (app.Settings.Gameplay != null)
            //{
            //    Gameplay = app.Settings.Gameplay;
            //}
            //else
            //{
            //    IDictionary<string, string> parameters = NavigationContext.QueryString;
            //    if (!parameters.ContainsKey("gameplay"))
            //    {
            //        throw new ArgumentException("QuerryString doesnot contain 'gameplay' key.");
            //    }

            //    switch (parameters["gameplay"])
            //    {
            //        case "single":
            //            Gameplay = new AIClassicGameplay();
            //            break;
            //        case "multi":
            //            Gameplay = new ClassicGameplay();
            //            break;
            //    }

            //    Gameplay.Page = this;
            //    Gameplay.Loaded += new System.Windows.RoutedEventHandler(OnGameplayLoaded);
            //}

            //Gameplay.Loaded += new RoutedEventHandler(OnGameplayLoaded);

            base.OnNavigatedTo(args);
        }

        /// <summary>
        /// Rời khỏi trang này.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedFrom(NavigationEventArgs args)
        {
            // Tắt âm thanh.
            //Gameplay.Game.SoundController.IsMute = true;

            // Xóa đối tượng Gamplay trong Settings.
            //(Application.Current as App).Settings.Gameplay = Gameplay;
            //Gameplay.Loaded -= OnGameplayLoaded;
            //gameplayGrid.Children.Clear();

            base.OnNavigatedFrom(args);
        }
        #endregion

        #region //--------- Event Handlers --------------//
        /// <summary>
        /// Bật hay tắt âm dựa theo settings.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected void OnGameplayLoaded(object sender, RoutedEventArgs args)
        {
            Gameplay.Page = this;
            Gameplay.Game.SoundController.IsMute = (Application.Current as App).IsMute;
            Gameplay.Game.StartGame();
        }
        #endregion

        private void OnBackKeyPress(object sender, CancelEventArgs args)
        {
            if (Gameplay.Game.CheckGameState() == GameState.NotFinished)
            {
                if (MessageBox.Show("Your game will not be saved, exit?", "Game is not finished", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Gameplay.Game.SoundController.IsMute = true;
                    this.NavigationService.GoBack();
                }
                else
                {
                    args.Cancel = true;
                }
            }
        }
    }
}